﻿using System;
using System.Windows.Forms;
using CRMS.Models;
using CRMS.Views.TeacherMgnt;

namespace CRMS.Views.StudentMgnt
{
    public partial class frmStudentMgnt : Form
    {
        //Field datas
        private DepartmentRep departRep = new DepartmentRep();
        private StudentRep studentRep = new StudentRep();
        private string departID;
        //Constructor
        public frmStudentMgnt(string departID)
        {
            InitializeComponent();
            this.departID = departID;
        }
        //Event handlers
        //选择在结果中搜索事件
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                TstripCboxCondition.Enabled = false;
            }
            else
            {
                TstripCboxCondition.Enabled = true;
            }
        }

        private void TstripBtnAdd_Click(object sender, EventArgs e)
        {
            txtStudentId.ReadOnly = false;
            txtStudentId.Text = String.Empty;
            txtStudentName.ReadOnly = false;
            txtStudentName.Text = String.Empty;
            comboBoxDepName.Enabled = true;
            txtStudentIndicate.ReadOnly = false;
            txtStudentIndicate.Text = String.Empty;
            comboBoxDepName.DataSource = departRep.GetDepartments();
            comboBoxDepName.DisplayMember = "DepartmentName";
            comboBoxDepName.ValueMember = "DepartmentId";
            txtStudentSex.ReadOnly = false;
            txtStudentSex.Text = String.Empty;

        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            txtStudentId.ReadOnly = true;
            txtStudentName.ReadOnly = false;
            txtStudentSex.ReadOnly = false;
            txtStudentIndicate.ReadOnly = false;
            comboBoxDepName.Enabled = true;
            this.TstripBtnSave.Click -= new EventHandler(this.TstripBtnSave_Click);
            this.TstripBtnSave.Click += new EventHandler(this.TstripBtnSave_Change);
            comboBoxDepName.DataSource = departRep.GetDepartments();
            comboBoxDepName.DisplayMember = "DepartmentName";
            comboBoxDepName.ValueMember = "DepartmentId";
        }

        private void TstripBtnSave_Change(object sender, EventArgs e)
        {
            Student student = new Student();
            student = studentRep.getStudentByID(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString());
            if (txtStudentId.Text.Trim() == String.Empty
                || txtStudentName.Text.Trim() == String.Empty
                || txtStudentSex.Text.Trim() == String.Empty
                || txtStudentIndicate.Text.Trim() == String.Empty)
            {
                MessageBox.Show("所有字段不能为空！");
            }
            else
            {
                DialogResult result = MessageBox.Show("是否确定修改？", "修改提示", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    student.StudentId = txtStudentId.Text.Trim();
                    student.StudentName = txtStudentName.Text.Trim();
                    student.StudentSex = txtStudentSex.Text.Trim();
                    student.StudentIndicate = txtStudentIndicate.Text.Trim();
                    student.DepartmentId = ((Department)comboBoxDepName.Items[comboBoxDepName.SelectedIndex]).DepartmentId;
                    studentRep.Save();
                    MessageBox.Show("修改成功！");
                    this.frmCourseMgnt_Load(sender, e);
                }
            }
        }

        private void TstripBtnSave_Click(object sender, EventArgs e)
        {
            Student student = new Student();
            if (txtStudentId.Text.Trim() == String.Empty
                || txtStudentName.Text.Trim() == String.Empty
                || txtStudentSex.Text.Trim() == String.Empty
                || txtStudentIndicate.Text.Trim() == String.Empty)
            {
                MessageBox.Show("所有字段不能为空！");
            }
            else
            {
                if (studentRep.getStudentByID(txtStudentId.Text.Trim()) != null)
                {
                    MessageBox.Show("该学生已经存在！");
                }
                else
                {
                    student.StudentId = txtStudentId.Text.Trim();
                    student.StudentName = txtStudentName.Text.Trim();
                    student.StudentSex = txtStudentSex.Text.Trim();
                    student.StudentIndicate = txtStudentIndicate.Text.Trim();
                    student.DepartmentId = ((Department)comboBoxDepName.Items[comboBoxDepName.SelectedIndex]).DepartmentId;
                    studentRep.Add(student);
                    studentRep.Save();
                    MessageBox.Show("添加成功！");
                    this.frmCourseMgnt_Load(sender, e);
                }
            }
        }

        private void TstripBtnDelete_Click(object sender, EventArgs e)
        {
            Student student = studentRep.getStudentByID(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString());

            if(student == null)
            {
                MessageBox.Show("该学生不存在或者已经被删除！");
            }
            else
            {
                DialogResult result = MessageBox.Show("您确定删除？", "删除提示", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    studentRep.Delete(student);
                    studentRep.Save();
                    MessageBox.Show("删除成功！");
                    this.frmCourseMgnt_Load(sender, e);
                }
            }
        }

        private void TstripBtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void frmCourseMgnt_Load(object sender, EventArgs e)
        {
            this.dataGridView1.AutoGenerateColumns = false;
            if (departID == String.Empty)
            {
                this.dataGridView1.DataSource = studentRep.getStudents();
            }
            else
            {
                this.dataGridView1.DataSource = studentRep.getStudentByDID(departID);
            }
        }

        private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellEventArgs e)
        {
            txtStudentId.ReadOnly = true;
            if (dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value == null)
            {
                return;
            }
            else
            {
                comboBoxDepName.Text = (departRep.GetDeptByID(dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString())).DepartmentName;
                txtStudentSex.Text = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtStudentName.Text = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtStudentId.Text = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtStudentIndicate.Text = dataGridView1[2, dataGridView1.CurrentCellAddress.Y].Value.ToString();
            }
        }

        private void TstripBtnLookUp_Click(object sender, EventArgs e)
        {
            string queryKeyword = TstripTxtKeyword.Text.Trim();
            if (TstripCboxCondition.Text == "学生姓名" && queryKeyword != string.Empty)
            {

                this.dataGridView1.DataSource = studentRep.getStudentsByName(queryKeyword);
                Promot();
            }
            else if (TstripCboxCondition.Text == "院系名称" && queryKeyword != string.Empty)
            {
                this.dataGridView1.DataSource = studentRep.getStudentsByDName(queryKeyword);
                Promot();
            }
            else
            {
                MessageBox.Show("请输入你的查询条件！");
            }
        }
        private void Promot()
        {
            if (this.dataGridView1.RowCount == 0)
            {
                MessageBox.Show("抱歉，未找到你要查的信息！");
            }
        }
   }
}
